#include "PenteBoard.h"
#include "defines.h"
#include <iostream>
using namespace std;

void PenteBoard::initializeBoard()
{
	whichPlayer = PLAYER_ONE;
	//iterate through all of the board nodes, handling each independently
	for( int i = 0; i < (BOARD_SIZE * BOARD_SIZE); i++ )
	{
		//smart auto-initialization of each tile
		/*
			  8 directional ptr node star
		  //--------------------------------
				   7 0 1
					\|/
				  6 -+- 2
					/|\
				   5 4 3
		*///--------------------------------
		
		//North
			//Top row, switching off all ptrs on the top of the node
			if( board[i]->getPos()->getX() == 0 )
			{
				//North side is null boundary
				board[i]->setDirectionalNodes(NORTH_WEST, NULL);
				board[i]->setDirectionalNodes(NORTH, NULL);
				board[i]->setDirectionalNodes(NORTH_EAST, NULL);
			
				//east: y+1
				//south east: x+1, y+1
				if( board[i]->getPos()->getY() != (BOARD_SIZE - 1) )
				{
					//get the array element number for the 
					int east1 = RowCol( board[i]->getPos()->getX() , board[i]->getPos()->getY() + 1  );
					int east2 = RowCol(  board[i]->getPos()->getX() + 1 , board[i]->getPos()->getY() + 1 );

					board[i]->setDirectionalNodes(EAST, board[east1]);
					board[i]->setDirectionalNodes(SOUTH_EAST, board[east2]);
				}
				//west: y-1
				//southwest: x+1, y-1
				if( board[i]->getPos()->getY() != 0 )
				{
					//get the array element number for the 
					int west1 = RowCol( board[i]->getPos()->getX() , board[i]->getPos()->getY() - 1  );
					int west2 = RowCol(  board[i]->getPos()->getX() + 1 , board[i]->getPos()->getY() - 1 );

					board[i]->setDirectionalNodes(WEST, board[west1]);
					board[i]->setDirectionalNodes(SOUTH_WEST, board[west2]);
				}
				//south: x+1
				if( board[i]->getPos()->getX() != (BOARD_SIZE - 1) )
				{
					int south = RowCol( board[i]->getPos()->getX() + 1 , board[i]->getPos()->getY() );
					board[i]->setDirectionalNodes(SOUTH, board[south]);
				}
			
			}
		//South
			//Bottom row, switching off all ptrs on the bottom of the node
			if( board[i]->getPos()->getX() == (BOARD_SIZE -1) )
			{
				board[i]->setDirectionalNodes(SOUTH_WEST, NULL);
				board[i]->setDirectionalNodes(SOUTH, NULL);
				board[i]->setDirectionalNodes(SOUTH_EAST, NULL);
			
				//east: y+1
				//north east: x-1, y+1
				if( board[i]->getPos()->getY() != (BOARD_SIZE - 1) )
				{
					//get the array element number for the 
					int east1 = RowCol( board[i]->getPos()->getX() , board[i]->getPos()->getY() + 1  );
					int east2 = RowCol(  board[i]->getPos()->getX() - 1 , board[i]->getPos()->getY() + 1 );

					board[i]->setDirectionalNodes(EAST, board[east1]);
					board[i]->setDirectionalNodes(NORTH_EAST, board[east2]);
				}
				//west: y-1
				//northwest: x-1, y-1
				if( board[i]->getPos()->getY() != 0 )
				{
					//get the array element number for the 
					int west1 = RowCol( board[i]->getPos()->getX() , board[i]->getPos()->getY() - 1  );
					int west2 = RowCol(  board[i]->getPos()->getX() - 1 , board[i]->getPos()->getY() - 1 );

					board[i]->setDirectionalNodes(WEST, board[west1]);
					board[i]->setDirectionalNodes(NORTH_WEST, board[west2]);
				}
				//north: x-1
				if( board[i]->getPos()->getX() != 0 )
				{
					int north = RowCol( board[i]->getPos()->getX() - 1 , board[i]->getPos()->getY() );
					board[i]->setDirectionalNodes(NORTH, board[north]);
				}
			
			}
		//East
			//Far right col, switching off all ptrs on the right side of the node
			if( board[i]->getPos()->getY() == (BOARD_SIZE - 1) )
			{
				board[i]->setDirectionalNodes(NORTH_EAST, NULL);
				board[i]->setDirectionalNodes(EAST, NULL);
				board[i]->setDirectionalNodes(SOUTH_EAST, NULL);
				//southwest: x+1, y-1
				//west: y-1
				//north west: x-1, y-1
				if( board[i]->getPos()->getY() != 0 )
				{
					//get the array element number for the 
					int west0 = RowCol( board[i]->getPos()->getX() + 1, board[i]->getPos()->getY() - 1 );
					int west1 = RowCol( board[i]->getPos()->getX() , board[i]->getPos()->getY() - 1  );
					int west2 = RowCol(  board[i]->getPos()->getX() - 1 , board[i]->getPos()->getY() - 1 );

					board[i]->setDirectionalNodes(SOUTH_WEST, board[west0]);
					board[i]->setDirectionalNodes(WEST, board[west1]);
					board[i]->setDirectionalNodes(NORTH_WEST, board[west2]);
				}
				//south: x+1
				if( board[i]->getPos()->getX() != (BOARD_SIZE - 1) )
				{
					int south = RowCol( board[i]->getPos()->getX() + 1 , board[i]->getPos()->getY() );
					board[i]->setDirectionalNodes(SOUTH, board[south]);
				}
				//north: x-1
				if( board[i]->getPos()->getX() != 0 )
				{
					int north = RowCol( board[i]->getPos()->getX() - 1 , board[i]->getPos()->getY() );
					board[i]->setDirectionalNodes(NORTH, board[north]);
				}
			
			} // end 
		//West
			//Far left col, switching off all ptrs on the left side of the node
			if( board[i]->getPos()->getY() == 0 )
			{
				board[i]->setDirectionalNodes(NORTH_WEST, NULL);
				board[i]->setDirectionalNodes(WEST, NULL);
				board[i]->setDirectionalNodes(SOUTH_WEST, NULL);
			
				//southeast: x+1, y+1
				//east: y+1
				//north east: x-1, y+1
				if( board[i]->getPos()->getY() != (BOARD_SIZE - 1) )
				{
					//get the array element number for the 
					int east0 = RowCol( board[i]->getPos()->getX() + 1, board[i]->getPos()->getY() + 1 );
					int east1 = RowCol( board[i]->getPos()->getX() , board[i]->getPos()->getY() + 1  );
					int east2 = RowCol(  board[i]->getPos()->getX() - 1 , board[i]->getPos()->getY() + 1 );

					board[i]->setDirectionalNodes(SOUTH_EAST, board[east0]);
					board[i]->setDirectionalNodes(EAST, board[east1]);
					board[i]->setDirectionalNodes(NORTH_EAST, board[east2]);
				}
				//south: x+1
				if( board[i]->getPos()->getX() != (BOARD_SIZE - 1) )
				{
					int south = RowCol( board[i]->getPos()->getX() + 1 , board[i]->getPos()->getY() );
					board[i]->setDirectionalNodes(SOUTH, board[south]);
				}
				//north: x-1
				if( board[i]->getPos()->getX() != 0 )
				{
					int north = RowCol( board[i]->getPos()->getX() - 1 , board[i]->getPos()->getY() );
					board[i]->setDirectionalNodes(NORTH, board[north]);
				}
			}// end west if()
		
		//All Directions active
		//----------------------------------------------------
			//if all ptrs are active, then initialize all ptrs
			if( board[i]->getPos()->getX() != 0 
				&& board[i]->getPos()->getX() != (BOARD_SIZE -1) 
				&& board[i]->getPos()->getY() != 0 
				&& board[i]->getPos()->getY() != (BOARD_SIZE - 1)
				)
			{
				//southeast: x+1, y+1
				//east: y+1
				//north east: x-1, y+1
				if( board[i]->getPos()->getY() != (BOARD_SIZE - 1) )
				{
					//get the array element number for the 
					int east0 = RowCol( board[i]->getPos()->getX() + 1, board[i]->getPos()->getY() + 1 );
					int east1 = RowCol( board[i]->getPos()->getX() , board[i]->getPos()->getY() + 1  );
					int east2 = RowCol(  board[i]->getPos()->getX() - 1 , board[i]->getPos()->getY() + 1 );

					board[i]->setDirectionalNodes(SOUTH_EAST, board[east0]);
					board[i]->setDirectionalNodes(EAST, board[east1]);
					board[i]->setDirectionalNodes(NORTH_EAST, board[east2]);
				}

				//southwest: x+1, y-1
				//west: y-1
				//north west: x-1, y-1
				if( board[i]->getPos()->getY() != 0 )
				{
					//get the array element number for the 
					int west0 = RowCol( board[i]->getPos()->getX() + 1, board[i]->getPos()->getY() - 1 );
					int west1 = RowCol( board[i]->getPos()->getX() , board[i]->getPos()->getY() - 1  );
					int west2 = RowCol(  board[i]->getPos()->getX() - 1 , board[i]->getPos()->getY() - 1 );

					board[i]->setDirectionalNodes(SOUTH_WEST, board[west0]);
					board[i]->setDirectionalNodes(WEST, board[west1]);
					board[i]->setDirectionalNodes(NORTH_WEST,  board[west2]);
				}
				//south: x+1
				if( board[i]->getPos()->getX() != (BOARD_SIZE - 1) )
				{
					int south = RowCol( board[i]->getPos()->getX() + 1 , board[i]->getPos()->getY() );
					board[i]->setDirectionalNodes(SOUTH,  board[south]);
				}
				//north: x-1
				if( board[i]->getPos()->getX() != 0 )
				{
					int north = RowCol( board[i]->getPos()->getX() - 1 , board[i]->getPos()->getY() );
					board[i]->setDirectionalNodes(NORTH, board[north]);
				}
			}//end all pieces if()
	}//end for()
	
	//proof of concept: each tile has the capability display the cursor or a board piece
	/*int temp = RowCol( 0,0 );
	board[temp]->setCursorOn();

	temp = RowCol( 1,1 );
	board[temp]->setPieceOnThisNode();*/


}//end InitializeBoard()
